package cn.itcast.service.system.impl;

import cn.itcast.dao.system.RoleDao;
import cn.itcast.dao.system.RoleDao;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Role> findAll(String companyId) {
        return roleDao.findAll(companyId);
    }

    @Override
    public void save(Role role) {
        roleDao.save(role);
    }

    @Override
    public Role findById(String id) {
        return roleDao.findById(id);
    }

    @Override
    public void update(Role role) {
        roleDao.updateById(role);
    }

    @Override
    public void deleteById(String id) {
        roleDao.deleteById(id);
    }

    @Override
    public PageInfo findPage(String companyId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Role> list = roleDao.findAll(companyId);
        return new PageInfo<>(list,5);

    }

    @Override
    @Transactional
    public void updateRoleModule(String roleid, String moduleIds) { //moduleIds '1212k34,35342,2323'
//        1、先删除这个角色之前的模块权限  delete from pe_role_module where role_id=#{roleid}
        roleDao.deleteRoleAndModuleByRoleId(roleid);
//      2、   保存角色和模块的关系 就是向中间表中插入数据
        String[] mIds = moduleIds.split(",");
        if(mIds!=null&&mIds.length>0){
            for (String mId : mIds) {
//                insert into pe_role_module(role_id,module_id) values (#{roleid},#{moduleid})
                roleDao.saveRoleAndModule(roleid,mId);
            }
        }
    }
}
